package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.ThesisDAO;
import edu.unf.cen6940.dao.ThesistoolDAO;

import edu.unf.cen6940.domain.Thesis;
import edu.unf.cen6940.domain.Thesistool;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Thesistool entities
 * 
 */

@Service("ThesistoolService")
@Transactional
public class ThesistoolServiceImpl implements ThesistoolService {

	/**
	 * DAO injected by Spring that manages Thesis entities
	 * 
	 */
	@Autowired
	private ThesisDAO thesisDAO;

	/**
	 * DAO injected by Spring that manages Thesistool entities
	 * 
	 */
	@Autowired
	private ThesistoolDAO thesistoolDAO;

	/**
	 * Instantiates a new ThesistoolServiceImpl.
	 *
	 */
	public ThesistoolServiceImpl() {
	}

	/**
	 * Return all Thesistool entity
	 * 
	 */
	@Transactional
	public List<Thesistool> findAllThesistools(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Thesistool>(thesistoolDAO.findAllThesistools(startResult, maxRows));
	}

	/**
	 * Load an existing Thesistool entity
	 * 
	 */
	@Transactional
	public Set<Thesistool> loadThesistools() {
		return thesistoolDAO.findAllThesistools();
	}

	/**
	 * Delete an existing Thesistool entity
	 * 
	 */
	@Transactional
	public void deleteThesistool(Thesistool thesistool) {
		thesistoolDAO.remove(thesistool);
		thesistoolDAO.flush();
	}

	/**
	 * Save an existing Thesis entity
	 * 
	 */
	@Transactional
	public Thesistool saveThesistoolThesises(Integer thesisToolId, Thesis related_thesises) {
		Thesistool thesistool = thesistoolDAO.findThesistoolByPrimaryKey(thesisToolId, -1, -1);
		Thesis existingthesises = thesisDAO.findThesisByPrimaryKey(related_thesises.getThesisId());

		// copy into the existing record to preserve existing relationships
		if (existingthesises != null) {
			existingthesises.setThesisId(related_thesises.getThesisId());
			existingthesises.setTitle(related_thesises.getTitle());
			existingthesises.setAbstract_(related_thesises.getAbstract_());
			existingthesises.setProposalAttempts(related_thesises.getProposalAttempts());
			existingthesises.setDefenseAttempts(related_thesises.getDefenseAttempts());
			existingthesises.setStartDate(related_thesises.getStartDate());
			existingthesises.setDoi(related_thesises.getDoi());
			related_thesises = existingthesises;
		} else {
			related_thesises = thesisDAO.store(related_thesises);
			thesisDAO.flush();
		}

		thesistool.getThesises().add(related_thesises);
		related_thesises.getThesistools().add(thesistool);
		thesistool = thesistoolDAO.store(thesistool);
		thesistoolDAO.flush();

		related_thesises = thesisDAO.store(related_thesises);
		thesisDAO.flush();

		return thesistool;
	}

	/**
	 * Return a count of all Thesistool entity
	 * 
	 */
	@Transactional
	public Integer countThesistools() {
		return ((Long) thesistoolDAO.createQuerySingleResult("select count(o) from Thesistool o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public Thesistool findThesistoolByPrimaryKey(Integer thesisToolId) {
		return thesistoolDAO.findThesistoolByPrimaryKey(thesisToolId);
	}

	/**
	 * Delete an existing Thesis entity
	 * 
	 */
	@Transactional
	public Thesistool deleteThesistoolThesises(Integer thesistool_thesisToolId, Integer related_thesises_thesisId) {
		Thesistool thesistool = thesistoolDAO.findThesistoolByPrimaryKey(thesistool_thesisToolId, -1, -1);
		Thesis related_thesises = thesisDAO.findThesisByPrimaryKey(related_thesises_thesisId, -1, -1);

		thesistool.getThesises().remove(related_thesises);
		related_thesises.getThesistools().remove(thesistool);
		thesistool = thesistoolDAO.store(thesistool);
		thesistoolDAO.flush();

		related_thesises = thesisDAO.store(related_thesises);
		thesisDAO.flush();

		thesisDAO.remove(related_thesises);
		thesisDAO.flush();

		return thesistool;
	}

	/**
	 * Save an existing Thesistool entity
	 * 
	 */
	@Transactional
	public void saveThesistool(Thesistool thesistool) {
		Thesistool existingThesistool = thesistoolDAO.findThesistoolByPrimaryKey(thesistool.getThesisToolId());

		if (existingThesistool != null) {
			if (existingThesistool != thesistool) {
				existingThesistool.setThesisToolId(thesistool.getThesisToolId());
				existingThesistool.setToolDescription(thesistool.getToolDescription());
			}
			thesistool = thesistoolDAO.store(existingThesistool);
		} else {
			thesistool = thesistoolDAO.store(thesistool);
		}
		thesistoolDAO.flush();
	}
}
